/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mingq
 */
public class ClientClient extends Thread {

    private Socket userSock;
    private DataInputStream reader;
    private DataOutputStream writer;
    private ChatMain mChatView;
    private ChatBox mChatBox;

    public ClientClient(String server, int port, ChatBox cb) {
        mChatBox = cb;
        if (userSock == null) {
            initUserSocket(server, port);
        }
    }

    public DataInputStream getReader() {
        return reader;
    }

    public DataOutputStream getWriter() {
        return writer;
    }

    public void sendMsg(String uid, String msg) {
        try {
            writer.write(Consts.MSG_START);
            writer.writeUTF(uid);
            writer.writeUTF(msg);
        } catch (IOException ex) {
            Logger.getLogger(ClientClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendShrink(String uid) {
        try {
            writer.write(Consts.SEND_SHRINK);
            writer.writeUTF(uid);
        } catch (IOException ex) {
            Logger.getLogger(ClientClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initUserSocket(String server, int port) {
        try {
            userSock = new Socket(server, port);
            InputStream in = userSock.getInputStream();
            reader = new DataInputStream(in);
            OutputStream out = userSock.getOutputStream();
            writer = new DataOutputStream(out);
        } catch (UnknownHostException ex) {
            LogManager.getLogger(ChatMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            LogManager.getLogger(ChatMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void startSendFile(String uid) {
        try {
            writer.write(Consts.FL_SEND);
        } catch (IOException ex) {
            Logger.getLogger(ClientClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendfile(String uid, String filename, String path) {
        try {
            File file = new File(path);
            int len = (int) file.length();
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[len];

            int bufferSize = 8192;
            byte[] buf = new byte[bufferSize];
            writer.write(Consts.Fl_START);
            writer.writeUTF(filename);
            while (true) {
                int read = 0;
                if (fis != null) {
                    read = fis.read(buf);
                }

                if (read == -1) {
                    break;
                }
                writer.write(buf, 0, read);
            }
            writer.flush();
            writer.write(Consts.FL_COMPELETE);
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ClientClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClientClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        super.run();

        try {
            int msg = Consts.EXIT;
            while ((msg = reader.read()) != Consts.EXIT) {
                switch (msg) {
                    case Consts.FL_CONFIRM_START:
                        sendfile("", mChatBox.mSelectedFileName, mChatBox.mSelectedPath);
                        break;
                    default:
                        break;
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
